package mars.activity;

import java.io.StringReader;
import java.net.URLEncoder;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import bear.download.Circleread;
import bear.download.HttpsUtils;
import bear.download.Searchread;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class Activity1 extends Activity {
	 private Button ok,land;
	 private EditText entry;
	 private RadioGroup rdGroup;
	 private RadioButton checkBox1,checkBox2,checkBox3;
	 private ScrollView scroll2;
	 private TextView roll10,roll11,roll12,roll13,roll14,roll15,roll16,roll17,roll18,roll19;
	 private int lead=0;
	 private boolean key1=false;
	 public static String danxuansou="";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedinstanceState) {
        super.onCreate(savedinstanceState);
        setContentView(R.layout.main);
        ok=(Button)findViewById(R.id.ok);
        land=(Button)findViewById(R.id.land);
        entry=(EditText)findViewById(R.id.entry);
        rdGroup=(RadioGroup)findViewById(R.id.rdGroup);
        checkBox1=(RadioButton)findViewById(R.id.checkBox1);
        checkBox2=(RadioButton)findViewById(R.id.checkBox2);
        checkBox3=(RadioButton)findViewById(R.id.checkBox3);
        scroll2 = (ScrollView)findViewById(R.id.sv0);
        scroll2.setVerticalScrollBarEnabled(false); 
        roll10=(TextView)findViewById(R.id.roll10);
		roll11=(TextView)findViewById(R.id.roll11);
		roll12=(TextView)findViewById(R.id.roll12);
		roll13=(TextView)findViewById(R.id.roll13);
		roll14=(TextView)findViewById(R.id.roll14);
		roll15=(TextView)findViewById(R.id.roll15);
		roll16=(TextView)findViewById(R.id.roll16);
		roll17=(TextView)findViewById(R.id.roll17);
		roll18=(TextView)findViewById(R.id.roll18);
		roll19=(TextView)findViewById(R.id.roll19);
		TextView landmessage=(TextView)findViewById(R.id.landmessage);
		
		checkBox1.setChecked(true);
		
        land.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View arg0) {
				// TODO Auto-generated method stub					
					if(key1==false){
			Intent intent=new Intent();
			intent.setClass(Activity1.this, landActivity.class);
			Activity1.this.startActivity(intent);
					}else{
						Intent intentuser=new Intent();
					intentuser.setClass(Activity1.this, User.class);
					Activity1.this.startActivity(intentuser);
					}
			}
			});
        rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkId) {
				// TODO Auto-generated method stub
				if(checkBox1.getId()==checkId){
				danxuansou="1";
				}else if(checkBox2.getId()==checkId){
					danxuansou="2";
				}
				else if(checkBox3.getId()==checkId){
					danxuansou="3";
				}
			}
        });        
        ok.setOnClickListener(new Button.OnClickListener()
        {
			public void onClick(View v) {
				String gjkey=entry.getText().toString();
				// TODO Auto-generated method stub
				 if("".equals(gjkey)) {
					Toast.makeText(Activity1.this, R.string.charater,Toast.LENGTH_LONG).show();
				}
				else {
					HttpsUtils sou=new HttpsUtils();
					String searchUrl=sou.get("https://www.loushiapp.com/?c=search&a=dosearch&type="+danxuansou+"&keyword="+URLEncoder.encode(gjkey));
					try{
						SAXParserFactory shou = SAXParserFactory.newInstance();
						XMLReader reader=shou.newSAXParser().getXMLReader();
						reader.setContentHandler(new Searchread());
						reader.parse(new InputSource(new StringReader(searchUrl)));
					}catch(Exception e){
						e.printStackTrace();
					}			   
					Intent intent=new Intent();
					intent.putExtra("gj",gjkey);
					intent.setClass(Activity1.this, Search.class);
					Activity1.this.startActivity(intent);
				}
			}	
        });
        key1=User.active;
		
		if(key1==true){
		landmessage.setText(getString(R.string.landmessaged));
		land.setText(R.string.returnuser);
		String sendnames[] = Circleread.sendname1.split("\\$");
		String loushiname[] = Circleread.sendname1.split("\\$");
		String pagecontents[]=Circleread.pagecontent1.split("\\$");
		if(Circleread.n==1){
		roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
		}
		if(Circleread.n==2){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			}
		if(Circleread.n==3){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			}
		if(Circleread.n==4){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			}
		if(Circleread.n==5){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			roll14.setText(sendnames[4]+" from "+loushiname[4]+":"+pagecontents[4]);
			}
		if(Circleread.n==6){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			roll14.setText(sendnames[4]+" from "+loushiname[4]+":"+pagecontents[4]);
			roll15.setText(sendnames[5]+" from "+loushiname[5]+":"+pagecontents[5]);
			}
		if(Circleread.n==7){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			roll14.setText(sendnames[4]+" from "+loushiname[4]+":"+pagecontents[4]);
			roll15.setText(sendnames[5]+" from "+loushiname[5]+":"+pagecontents[5]);
			roll16.setText(sendnames[6]+" from "+loushiname[6]+":"+pagecontents[6]);
			}
		if(Circleread.n==8){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			roll14.setText(sendnames[4]+" from "+loushiname[4]+":"+pagecontents[4]);
			roll15.setText(sendnames[5]+" from "+loushiname[5]+":"+pagecontents[5]);
			roll16.setText(sendnames[6]+" from "+loushiname[6]+":"+pagecontents[6]);
			roll17.setText(sendnames[7]+" from "+loushiname[7]+":"+pagecontents[7]);
			}
		if(Circleread.n==9){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			roll14.setText(sendnames[4]+" from "+loushiname[4]+":"+pagecontents[4]);
			roll15.setText(sendnames[5]+" from "+loushiname[5]+":"+pagecontents[5]);
			roll16.setText(sendnames[6]+" from "+loushiname[6]+":"+pagecontents[6]);
			roll17.setText(sendnames[7]+" from "+loushiname[7]+":"+pagecontents[7]);
			roll18.setText(sendnames[8]+" from "+loushiname[8]+":"+pagecontents[8]);
			}
		if(Circleread.n==10){
			roll10.setText(sendnames[0]+" from "+loushiname[0]+":"+pagecontents[0]);
			roll11.setText(sendnames[1]+" from "+loushiname[1]+":"+pagecontents[1]);
			roll12.setText(sendnames[2]+" from "+loushiname[2]+":"+pagecontents[2]);
			roll13.setText(sendnames[3]+" from "+loushiname[3]+":"+pagecontents[3]);
			roll14.setText(sendnames[4]+" from "+loushiname[4]+":"+pagecontents[4]);
			roll15.setText(sendnames[5]+" from "+loushiname[5]+":"+pagecontents[5]);
			roll16.setText(sendnames[6]+" from "+loushiname[6]+":"+pagecontents[6]);
			roll17.setText(sendnames[7]+" from "+loushiname[7]+":"+pagecontents[7]);
			roll18.setText(sendnames[8]+" from "+loushiname[8]+":"+pagecontents[8]);
			roll19.setText(sendnames[9]+" from "+loushiname[9]+":"+pagecontents[9]);
			}
		}
		if(key1==false){
			landmessage=null;
		}
    }
        @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0,1,1,R.string.exit);
		menu.add(0,2,2,R.string.options);
		menu.add(0,3,3,R.string.help);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId()==1){
			AlertDialog dialog = new AlertDialog.Builder(Activity1.this)  
            .setIcon(android.R.drawable.btn_star_big_on)  
            .setTitle(R.string.tuichu)  
          .setPositiveButton(R.string.determine, onclick)  
            .setNegativeButton(R.string.cancel,  onclick).create();  
				dialog.show(); 
		}else if(item.getItemId()==2){
			item.setIntent(new Intent(this, Options.class));
		}else if(item.getItemId()==3){
			item.setIntent(new Intent(this, Help.class));
		}
		return super.onOptionsItemSelected(item);
	}
	private Handler message2 = new Handler(){   
        public void handleMessage(Message msg) {   
            doScrow();                     
            }
	};
	public class TimerLoop implements Runnable {   
        public void run() {   
            // TODO Auto-generated method stub                 
            while(true){   
                loop(200000);                     
                message2.sendEmptyMessage(0);   
            }   
        }              
    }     
    public void loop(long i){   
        long j = i;   
        while(j>0){                 
            j = j-1;   
        }     
    }    
    public boolean onKeyDown(int keyCode, KeyEvent msg){   
        Thread loop = new Thread(new TimerLoop());   
        loop.start();              
        return super.onKeyDown(keyCode, msg);   
    }  
    public void doScrow(){   
        int now = scroll2.getScrollY(); 
        if(lead == now){   
            scroll2.scrollTo(now, 0);   
            lead = -1;                  
        }   
        else {   
            scroll2.smoothScrollBy(0,1);                  
            lead = now;                  
        }   
    }
    OnClickListener onclick = new OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) {  
            switch (which) {  
                case Dialog.BUTTON_NEGATIVE:  
                    break;  
                case Dialog.BUTTON_POSITIVE:
                	Intent startMain = new Intent(Intent.ACTION_MAIN);
        			startMain.addCategory(Intent.CATEGORY_HOME);
        			startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        			startActivity(startMain);
        			System.exit(0);
                    break;  
            } 
        }
    };
}